package com.lsl.agency;

import cn.hutool.aop.ProxyUtil;
import cn.hutool.aop.aspects.TimeIntervalAspect;
import cn.hutool.http.HttpUtil;
import org.apache.tools.ant.taskdefs.optional.pvcs.Pvcs;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**动态代理示例
 * @author lishuailong
 * @create 2020-07-17 10:19
 */
public class test2 {
    public static void main(String[] args) {

        SuperMan superMan = new SuperMan();
        Human proxyInstance = (Human) ProxyFactory.getProxyInstance(superMan);
        proxyInstance.eat("蔬菜");
        String belief = proxyInstance.getBelief();
        System.out.println(belief);


        A proxy = ProxyUtil.proxy(new A(), TimeIntervalAspect.class);
        proxy.aVoid();
    }
}
class A{
    public void aVoid(){
        System.out.println("123");
    }
}

/**
 * 人 接口
 */
interface Human{
 String getBelief();
 void eat(String food);
}

/**
 * 被代理类
 */
class SuperMan implements Human {
    @Override
    public String getBelief() {
        return "保护世界，爱好和平！";
    }

    @Override
    public void eat(String food) {
        System.out.println("我喜欢吃"+food);
    }
}

/**
 * 代理工厂
 */
class ProxyFactory{
    /** 获得一个动态代理类
     * @param o 被代理类对象
     * @return 返回一个动态代理类
     */
    public static Object getProxyInstance(Object o){
        return Proxy.newProxyInstance(o.getClass().getClassLoader(), o.getClass().getInterfaces(), new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                return method.invoke(o, args);
            }
        });
    }
}

/**
 * 代理类
 */
 class ProxyTest{


 }
